<template>
	<view class="container">
		<view class="header">
			<text class="title">{{excel.name}}</text>
			<text class="date">创建于 {{excel.createTime}}</text>
		</view>
		
		<scroll-view class="table-container" scroll-x :enable-flex="false" :show-scrollbar="true">
			<view class="table" :style="{ minWidth: tableMinWidth + 'px' }">
				<view class="table-header">
					<view class="th" v-for="(field, index) in getFieldsArray()" :key="index">
						{{getFieldName(field)}}
					</view>
				</view>
				
				<view class="table-body">
					<view v-if="excel.data.length === 0" class="empty-state">
						<text class="empty-text">暂无数据</text>
					</view>
					
					<view v-else class="tr" v-for="(row, rowIndex) in excel.data" :key="rowIndex">
						<view class="td" v-for="(field, fieldIndex) in getFieldsArray()" :key="fieldIndex">
							{{row[getFieldName(field)] || ''}}
						</view>
					</view>
				</view>
			</view>
		</scroll-view>
		
		<view class="scroll-tip" v-if="getFieldsArray().length > 3">
			<text class="tip-text">← 左右滑动查看更多字段 →</text>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			excel: {
				name: '',
				createTime: '',
				fields: [],
				data: []
			}
		}
	},
	computed: {
		// 计算表格最小宽度
		tableMinWidth() {
			const fieldCount = this.getFieldsArray().length;
			const minColumnWidth = 150; // 每列最小宽度
			return Math.max(fieldCount * minColumnWidth, 400); // 至少400px宽度
		}
	},
	onLoad(options) {
		const { id } = options
		this.loadExcelData(id)
	},
	methods: {
		loadExcelData(id) {
			const excelList = uni.getStorageSync('excelList') || []
			const excel = excelList.find(item => item.id === id)
			if (excel) {
				this.excel = excel
			} else {
				uni.showToast({
					title: '未找到表格数据',
					icon: 'none'
				})
				setTimeout(() => {
					uni.navigateBack()
				}, 1500)
			}
		},
		// 获取字段数组，兼容旧版本数据格式
		getFieldsArray() {
			if (!this.excel.fields || this.excel.fields.length === 0) {
				return [];
			}
			return this.excel.fields;
		},
		// 获取字段名称，兼容旧版本和新版本数据格式
		getFieldName(field) {
			if (typeof field === 'string') {
				// 旧版本格式：字段是字符串
				return field;
			} else if (typeof field === 'object' && field.name) {
				// 新版本格式：字段是对象
				return field.name;
			}
			return '';
		}
	}
}
</script>

<style>
.container {
	padding: 20px;
	min-height: 100vh;
	background-color: #f8f8f8;
	display: flex;
	flex-direction: column;
}

.header {
	margin-bottom: 20px;
}

.title {
	font-size: 20px;
	font-weight: bold;
	color: #333;
	display: block;
	margin-bottom: 5px;
}

.date {
	font-size: 12px;
	color: #999;
}

.table-container {
	flex: 1;
	background: #fff;
	border-radius: 12px;
	padding: 15px;
	overflow-x: auto;
	overflow-y: auto;
}

.table {
	border: 1px solid #eee;
	border-radius: 8px;
	overflow: hidden;
	width: 100%;
}

.table-header {
	display: flex;
	background-color: #f5f5f5;
	flex-wrap: nowrap;
}

.th {
	min-width: 150px;
	width: 150px;
	flex-shrink: 0;
	padding: 12px;
	font-size: 14px;
	font-weight: 500;
	color: #333;
	text-align: left;
	border-right: 1px solid #eee;
	white-space: nowrap;
	overflow: hidden;
	text-overflow: ellipsis;
}

.th:last-child {
	border-right: none;
}

.table-body {
	background: #fff;
}

.tr {
	display: flex;
	border-top: 1px solid #eee;
	flex-wrap: nowrap;
}

.tr:nth-child(even) {
	background-color: #f9f9f9;
}

.td {
	min-width: 150px;
	width: 150px;
	flex-shrink: 0;
	padding: 12px;
	font-size: 14px;
	color: #666;
	border-right: 1px solid #eee;
	white-space: nowrap;
	overflow: hidden;
	text-overflow: ellipsis;
	word-break: break-all;
}

.td:last-child {
	border-right: none;
}

.empty-state {
	padding: 40px 0;
	text-align: center;
	grid-column: 1 / -1;
}

.empty-text {
	color: #999;
	font-size: 14px;
}

.scroll-tip {
	padding: 10px 0;
	text-align: center;
}

.tip-text {
	font-size: 12px;
	color: #999;
	background-color: rgba(0, 122, 255, 0.1);
	padding: 5px 15px;
	border-radius: 15px;
}
</style> 